//
//  videoViewController.m
//  tangDynasty
//
//  Created by 洪闽程 on 2018/11/15.
//  Copyright © 2018年 洪闽程. All rights reserved.
//

#import "videoViewController.h"
#import <MJExtension.h>
#import "MDHttpService.h"
#import "CLPlayerView.h"
#import "UIView+CLSetRect.h"
#import "dataModel.h"
#import "MDObjManager.h"
@interface videoViewController ()<CALayerDelegate>
/**CLplayer*/
@property (nonatomic,weak) CLPlayerView *playerView;
/**<##>*/
@property (nonatomic,strong)NSArray <dataModel *>*dataArr;

/**<##>*/
@property (nonatomic,strong)NSMutableArray * collectArr;

/**<##>*/
@property (nonatomic,strong)NSArray * urlArr;


@end

@implementation videoViewController

-(NSMutableArray *)collectArr
{
    if (!_collectArr)
    {
        
     
        
        if ([NSKeyedUnarchiver unarchiveObjectWithFile:[MDObjManager KeyedArchiverPathName:@"data.text"]])
        {
            _collectArr=[NSKeyedUnarchiver unarchiveObjectWithFile:[MDObjManager KeyedArchiverPathName:@"data.text"]];
        }
         else
         {
            _collectArr =[NSMutableArray array];
         }
      
       
//        NSString *filePath = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//
//        NSString *newFilePath = [filePath stringByAppendingPathComponent:@"damodel.txt"];
      

    }
    return _collectArr;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
 
  
    NSString *path=[[NSBundle mainBundle]pathForResource:@"URL" ofType:@"plist"];
    
    self.urlArr=[NSArray arrayWithContentsOfFile:path];
}


-(void)setModel:(dataModel *)model
{
    CLPlayerView *playerView = [[CLPlayerView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width)];
    _playerView = playerView;
    
    [self.view addSubview:_playerView];
    
    [_playerView updateWithConfig:^(CLPlayerViewConfig *config) {
        //当前控制器是否支持旋转，当前页面支持旋转的时候需要设置，告知播放器
        config.isLandscape = YES;
        //全屏是否隐藏状态栏，默认一直不隐藏
        config.fullStatusBarHiddenType = FullStatusBarHiddenNever;
        //顶部工具条隐藏样式，默认不隐藏
        config.topToolBarHiddenType = TopToolBarHiddenNever;
        //全屏手势控制，默认Yes
        config.fullGestureControl = NO;
        //小屏手势控制，默认NO
        config.smallGestureControl = YES;
        config.progressBackgroundColor=[UIColor redColor];
    }];
    
    
    //返回按钮点击事件回调,小屏状态才会调用，全屏默认变为小屏
    [_playerView backButton:^(UIButton *button) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [_playerView shareButton:^(UIButton *button) {
        
        UIImage *image=[UIImage imageNamed:@"删除"];
        
        UIActivityViewController *activityController=[[UIActivityViewController alloc]initWithActivityItems:@[image] applicationActivities:nil];
        [self presentViewController:activityController animated:YES completion:nil];
        
        
    }];
    
    [_playerView collectButton:^(UIButton *button)
     {

         dataModel *myModel=[[dataModel alloc]init];
         myModel.name=model.name;
         myModel.url=model.url;
         
         [self.collectArr addObject:myModel];
         
         //如果success为true则代表成功
         
         BOOL success = [NSKeyedArchiver archiveRootObject:self.collectArr toFile:[MDObjManager KeyedArchiverPathName:@"data.text"]];
     
         if (success)
         {
             NSLog(@"归档成功");
         }
         else
         {
             NSLog(@"归档失败");
         }
        
        
         
     }];
    
    
    MDHttpService *httpService=[MDHttpService shareHttpService];
    
    NSString *url=@"http://www.wuqiongda8888.com/video/detailList.htm";
    NSDictionary *param=@{@"appId":@"75",@"id":model.id,@"pageSize":@"100000"};
    
    [httpService postUrlStr:url andparam:param andsuccess:^(NSDictionary *dic) {
  
        self.dataArr=[dataModel mj_objectArrayWithKeyValuesArray:dic[@"content"][@"videoDetails"]];
        
        _playerView.dataItems=self.dataArr;
        
    } andfailed:^(MError *error)
     {
         
         
         
     }];
    
    
    NSLog(@"%@------%ld",_urlArr[self.selectRow][0],self.selectRow);
    //视频地址
    _playerView.url= [NSURL URLWithString:_urlArr[self.selectRow][0]];
    
    _playerView.selectBlock = ^(NSInteger index) {

    _playerView.url=[NSURL URLWithString:_urlArr[self.selectRow][index]];
          NSLog(@"------%ld",self.selectRow);
    //播放
    [_playerView playVideo];
    };

    //播放
    [_playerView playVideo];
    
    //播放完成回调
    [_playerView endPlay:^{
        NSLog(@"播放完成");
    }];
}




-(void)viewDidDisappear:(BOOL)animated{
    [_playerView destroyPlayer];
}


// 支持哪些屏幕方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeRight;
}
// 默认的屏幕方向（当前ViewController必须是通过模态出来的UIViewController（模态带导航的无效）方式展现出来的，才会调用这个方法）
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeRight;
}



@end
